package com.xiaoming.controller;

import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaoming
 * @Date 2020/3/26
 */
@Slf4j
@RestController
@RequestMapping("/wx")
public class WeiXinServiceImpl {

    @Autowired
    private WxMpService wxService;



    @GetMapping("/getOpenId")
    public String getOpenId() throws WxErrorException {
        String accessToken = wxService.getAccessToken();
        return accessToken;
    }

    @PostMapping("/sendTemplate")
    public String sendTemplate(@RequestBody WxMpTemplateMessage wxMpTemplateMessage) {
        log.info("###sendTemplate###wxMpTemplateMessage={}", wxMpTemplateMessage.toJson());
        WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
        try {
            return templateMsgService.sendTemplateMsg(wxMpTemplateMessage);
        } catch (WxErrorException e) {
            log.error("WeiXinServiceImpl### ERROR:", e);
            return null;
        }
    }
}
